package dao;

import application.exceptions.ItemNotFoundException;
import mappings.Material;
import mappings.Producent;
import mappings.ProducentComposition;
import mappings.Standards;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;
import utils.HibernateUtil;

import java.util.List;

public class ProducentCompositionDao
{
    private static SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

    public List<Producent> getProducentsForMaterial(Integer materialId)
    {
        Session session = sessionFactory.openSession();

        List<Producent> producents = session.createQuery("select p from Producent p left join p.producentCompositions c where c.material.id = :materialId")
                .setParameter("materialId", materialId)
                .list();

        session.close();
        return producents;
    }

    public ProducentComposition addProducentComposition(int materialId, int producentId) throws ItemNotFoundException
    {
        ProducentComposition producentComposition = new ProducentComposition();

        org.hibernate.classic.Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();

        Material material = (Material) session.get(Material.class, materialId);
        Producent producent = (Producent) session.get(Producent.class, producentId);

        if(material == null || producent == null)
        {
            tx.rollback();
            session.close();
            throw new ItemNotFoundException();
        }

        producentComposition.setMaterial(material);
        producentComposition.setProducent(producent);

        session.save(producentComposition);

        tx.commit();
        session.close();

        return producentComposition;
    }

    public void deleteProducentComposition(Integer id) throws ItemNotFoundException
    {
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();

        int affectedRows = session.createQuery("delete ProducentComposition where id = :id")
                .setParameter("id", id)
                .executeUpdate();

        if(affectedRows == 0)
        {
            tx.rollback();
            session.close();
            throw new ItemNotFoundException();
        }

        tx.commit();
        session.close();
    }

    public ProducentComposition editProducentComposition(Integer id, int materialId, int producentId) throws ItemNotFoundException
    {
        org.hibernate.classic.Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();

        ProducentComposition producentComposition = (ProducentComposition) session.get(ProducentComposition.class, id);
        Material material = (Material) session.get(Material.class, materialId);
        Producent producent = (Producent) session.get(Producent.class, producentId);

        if(producentComposition == null || material == null || producent == null)
        {
            tx.rollback();
            session.close();
            throw new ItemNotFoundException();
        }

        producentComposition.setMaterial(material);
        producentComposition.setProducent(producent);
        session.update(producentComposition);
        tx.commit();
        session.close();

        return producentComposition;
    }
}
